
/*

Dofile: table4_appendix.do 

Date: Dec. 10, 2016 
Aim: Stat des on the composition of intra-firm exports to tax havens and non-havens

Input: base_for_reg.dta 
Output: table4_appendix.dta 
Reported in the online appendix: "Table A.4: Composition of intra-firm exports, tax havens and ROW (2-digit)"

*/  

cd $datapath

use "W:\research\data\Tax\Tax\cadre-b-c.dta" , clear
destring siren, replace force
drop if cadre==" "
drop if cadre==""
drop if cadre=="p"
drop if siren==0
drop if siren==46
drop if siren==1459
drop if siren==82001
replace cadre="Import" if cadre=="C"
replace cadre="Export" if cadre=="B"
keep if cadre=="Export"
drop if siren==.
sort siren
drop if extra<0
keep if intra>98 

g haven=0 
foreach x in LU CH IE HK CY SG MT BS BM KY{
replace haven=1 if  pays=="`x'" 
}

g nope=fab=="X"
egen tot =sum(montant) , by(nope)

foreach i in rev fab fin inv{
replace `i'="0" if `i'==""
}

drop if nope==1 

 g hs2=substr(sh,1,2)
 g export=montant
collapse (sum) export, by(sh haven) 
reshape wide export, i(sh) j(haven) 
egen tot1=sum(export1)
egen tot0=sum(export0) 
g sh1=export1/tot1
g sh0=export0/tot0 
sort sh1
save $resultpath/table4_appendix.dta , replace 
